home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / Threading / threadinterrupt.dpr < prev    next >
Encoding:
Text File  |  2004-10-22  |  1.2 KB  |  54 lines

  1. program threadinterrupt;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. //
  6. // This example demonstrates how to raise an ThreadInterruptedException in
  7. // another thread. 
  8. //
  9. // Written by: Rick Ross (http://www.rick-ross.com)
  10. //
  11.  
  12. uses System.Threading;
  13.  
  14. type
  15.   TMyThread = class
  16.   public
  17.     procedure MyThreadMethod;  
  18.   end;
  19.  
  20. procedure TMyThread.MyThreadMethod;
  21. begin
  22.   System.Console.WriteLine('Staring MyThreadMethod...');
  23.   try
  24.     System.Console.WriteLine('Resting for a while..');
  25.     Thread.Sleep(Timeout.Infinite);
  26.     System.Console.WriteLine('This will never be seen!');
  27.   except
  28.     on e : System.Threading.ThreadInterruptedException do
  29.     begin
  30.       System.Console.WriteLine('thread has been interrupted!');        
  31.     end;
  32.   end;
  33.  
  34.   System.Console.WriteLine('Yawn...Why did you wake me up?');
  35. end;
  36.  
  37. var
  38.   tc : TMyThread;
  39.   t  : Thread;
  40.  
  41. begin
  42.   System.Console.WriteLine('Starting up...');
  43.   tc := TMyThread.Create;
  44.   System.Console.WriteLine('creating thread...'); 
  45.   t := Thread.Create( @tc.MyThreadMethod );  
  46.   System.Console.WriteLine('Press ENTER to interrupt the thread from its nap!');
  47.  
  48.   t.Start();
  49.  
  50.   readln;
  51.   t.Interrupt();
  52. end.
  53.  
  54.